<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/3/3
 * Time: 8:10
 */

namespace app\api\home;

use \app\common\model\User as userModel;

class Login extends Base
{
    /**
     * 手机注册
     * @param string phone 号码
     * @param string code 验证码
     * @param string password 密码
     * @param string repassword 确认密码
     * @return string
     */
    public function register()
    {
        //有效参数
        $value = app('request')->only(['phone','code','password','repassword']);

        $validate = new \app\api\validate\User();

        if(!$validate->scene('register')->check($value)){
            return $this->ajaxError($validate->getError());
        }

        $return = userModel::add(0,$value['phone'],md5($value['password']));

        if($return['code']){
            return $this->ajaxError($return['error']);
        }

        return $this->ajaxReturn('',$return['msg']);
    }

    /**
     * 手机登录
     * @param string phone 号码
     * @param string password 密码
     * @return string
     * @throws \think\exception\DbException
     */
    public function login(){
        //有效参数
        $value = app('request')->only(['phone','password']);

        $validate = new \app\api\validate\User();

        if(!$validate->scene('login')->check($value)){
            return $this->ajaxError($validate->getError());
        };

        $where = [
            'phone'=>$value['phone'],
            'password'=>md5($value['password'])
        ];

        $user = userModel::where($where)->find();

        if(empty($user->id)){
            return $this->ajaxError('账号或密码错误');
        }

        return $this->ajaxReturn($user,'登陆成功');
    }
}